//
//  DetailViewController.m
//  SecureNotes
//
//  Created by 杨岑 on 10/4/15.
//  Copyright © 2015 杨岑. All rights reserved.
//

#import "DetailViewController.h"
@import LocalAuthentication;


@interface DetailViewController ()
@property (weak, nonatomic) IBOutlet UITextView *textView;
@end

@implementation DetailViewController
{
    NSMutableArray* _toolbarItems;
}

#pragma mark - Managing the detail item

- (void)setDetailItem:(id)newDetailItem {
    if (_detailItem != newDetailItem) {
        _detailItem = newDetailItem;
            
        // Update the view.
        [self configureView];
    }
}

- (void)configureView {
    // Update the user interface for the detail item.
    if (self.detailItem) {
        self.detailDescriptionLabel.text = [self.detailItem description];
        self.title = self.detailDescriptionLabel.text;
    }
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    [self configureView];
    [self registerForKeyboardNotifications];
    
    UIBarButtonItem* item1 = [UIBarButtonItem new];
    item1.title = @"Tool1";
    self.toolbarItems = @[item1];
    
    UINavigationController* nc = (UINavigationController*)self.parentViewController;
    [nc setToolbarHidden:NO animated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (IBAction)test_click:(id)sender {
    LAContext* lactx = [LAContext new];
    NSError* error = nil;
    
    if ([lactx canEvaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics error:&error])
    {
        NSLog(@"Touch ID available");
        
        //lactx.localizedFallbackTitle = @"Enter passcode";
        [lactx evaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics localizedReason:@"Test Touch ID" reply:^(BOOL success, NSError * _Nullable error) {
            
            if (success)
            {
                NSLog(@"Touch ID OK");
            }
            else
            {
                NSLog(@"%@", error);
            }
        }];
    }
    else
    {
        NSLog(@"Touch ID NOT available: %@", error);
    }
}

- (void)registerForKeyboardNotifications
{
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(keyboardWasShown:)
                                                 name:UIKeyboardDidShowNotification object:nil];
    
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(keyboardWillBeHidden:)
                                                 name:UIKeyboardWillHideNotification object:nil];
    
}

// Called when the UIKeyboardDidShowNotification is sent.
- (void)keyboardWasShown:(NSNotification*)aNotification
{
    NSDictionary* info = [aNotification userInfo];
    CGSize kbSize = [[info objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue].size;
    NSLog(@"Keyboard size: %.1f %.1f", kbSize.width, kbSize.height);
   
    UIEdgeInsets contentInsets = UIEdgeInsetsMake(0.0, 0.0, kbSize.height, 0.0);
    self.textView.contentInset = contentInsets;
    self.textView.scrollIndicatorInsets = contentInsets;
    
    // If active text field is hidden by keyboard, scroll it so it's visible
    // Your app might not need or want this behavior.
    CGRect aRect = self.view.frame;
    aRect.size.height -= kbSize.height;
    if (!CGRectContainsPoint(aRect, self.textView.frame.origin) ) {
        [self.textView scrollRectToVisible:self.textView.frame animated:YES];
    }
}

// Called when the UIKeyboardWillHideNotification is sent
- (void)keyboardWillBeHidden:(NSNotification*)aNotification
{
    UIEdgeInsets contentInsets = UIEdgeInsetsZero;
    self.textView.contentInset = contentInsets;
    self.textView.scrollIndicatorInsets = contentInsets;
}

- (void)textFieldDidBeginEditing:(UITextField *)textField
{
    
}

- (void)textFieldDidEndEditing:(UITextField *)textField
{
    
}

@end
